﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Silvermoon.Navigation;
using System.Drawing;
using System.Threading;

namespace Silvermoon.WinControls
{
    public delegate Silvermoon.Controls.Window CreateWindow();

    public class HostForm : Form
    {

        private Silvermoon.Core.Screen screen;
        private Label label;
        private global::System.Windows.Forms.Timer timer;

        public CreateWindow CreateWindow;

        internal HostForm()
            : base()
        {
            AutoScaleMode = AutoScaleMode.Dpi;
            MinimizeBox = false;
            WindowState = FormWindowState.Maximized;
            FormBorderStyle = FormBorderStyle.None;

            label = new Label { Text = "loading...", TextAlign = ContentAlignment.TopCenter, ForeColor = Color.White };
            LayoutLabel();
            Controls.Add(label);
            BackColor = Color.Black;
            ForeColor = Color.White;
            timer = new System.Windows.Forms.Timer { Interval = 5 };
            timer.Tick += new EventHandler(OnTimerTick);
            timer.Enabled = true;
        }

        void OnTimerTick(object sender, EventArgs e)
        {
            timer.Tick -= OnTimerTick;
            timer.Enabled = false;
            timer.Dispose();
            timer = null;
            InitializeSilvermoon();

        }

        protected override void OnResize(EventArgs e)
        {
            if (label != null)
            {
                LayoutLabel();
            }
            base.OnResize(e);
        }


        private void LayoutLabel()
        {
            int w = this.Width;
            int h = this.Height;
            label.Bounds = new Rectangle(0, (h - label.Height) / 2, w, 48);
        }


        protected override void OnPaint(PaintEventArgs e)
        {
            if (screen == null) base.OnPaint(e);
            else
            {
                screen.Invalidate();
                screen.Update();
            }
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            if (screen == null) base.OnPaintBackground(e);
        }


        private void InitializeSilvermoon()
        {
            if (Silvermoon.Core.Screen.Current == null)
            {
                var screen = Silvermoon.Core.Screen.Initialize(this);
                screen.Renderer.BackgroundColor = Color.Black;
                screen.Renderer.Perspective = true;

                Silvermoon.Controls.Window window = CreateHomePage();
                if (window != null)
                {
                    window.PreLoad();
                    window.Invalidate(Silvermoon.Controls.InvalidationFlag.Appearance | Silvermoon.Controls.InvalidationFlag.Size);
                    this.screen = screen;
                    Controls.Remove(label);
                    Thread.CurrentThread.Priority = ThreadPriority.BelowNormal;
                    Invalidate();
                    window.Schedule(() =>
                    {
                        Silvermoon.Controls.Page page = window as Silvermoon.Controls.Page;
                        if (page != null)
                        {
                            page.NavigateTo(page);
                        }
                        else
                        {
                            window.Show();
                        }
                    });
                }
            }
        }

        protected virtual Silvermoon.Controls.Window CreateHomePage()
        {
            if (CreateWindow != null) return CreateWindow();
            return null;
        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {

            if (Visible)
            {
                var screen = Silvermoon.Core.Screen.Current;
                if (screen != null)
                {
                    e.Cancel = true;
                    screen.RaiseCloseEvent();
                }
            }
            base.OnClosing(e);
        }




    }
}
